package com.example.weatherapp.adapter;

import static android.content.Context.MODE_PRIVATE;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.weatherapp.R;

import java.util.List;

public abstract class ThemeAdapter extends RecyclerView.Adapter<ThemeAdapter.RVHolder> {

    private List<Integer> datas;
    private Integer theme;

    public ThemeAdapter(Context context, List<Integer> datas) {
        this.datas = datas;
        SharedPreferences preferences = context.getSharedPreferences("cities", MODE_PRIVATE);
        theme = preferences.getInt("theme", R.mipmap.bg_main_theme1);
    }

    public abstract void selectTheme(Integer theme);

    @NonNull
    @Override
    public RVHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_theme, parent, false);
        return new RVHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RVHolder holder, int position) {
        holder.ivTheme.setImageResource(datas.get(position));
        holder.tvName.setText((theme.equals(datas.get(position)) ? "当前主题" : "主题" + (position + 1)));
        holder.itemView.setOnClickListener(v -> selectTheme(datas.get(position)));
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public class RVHolder extends RecyclerView.ViewHolder {
        private ImageView ivTheme;
        private TextView tvName;

        public RVHolder(@NonNull View itemView) {
            super(itemView);
            ivTheme = itemView.findViewById(R.id.ivTheme);
            tvName = itemView.findViewById(R.id.tvName);
        }
    }
}
